package bzapps.weynak;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class BatteryService extends Service {
	
	private int currentPercent = 0;
	public static boolean isRunning;
	 
	private SharedPreferences pref;
	 
	private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			
			String batt_level = pref.getString("weynak.power_saving_levels", "20");
			int level = intent.getIntExtra("level", 0);
            int scale = intent.getIntExtra("scale", 100);
            int plugged = intent.getIntExtra("plugged", 100);
            
            currentPercent = level * 100 / scale;
           
            Log.d("WEYNAK.battery.level", String.valueOf(currentPercent)+"%");
            
            if(currentPercent <= Integer.parseInt(batt_level) && plugged==0 && !Maps.batterywarning) 
            {
               	Maps.utils.sendEmptyMessage(Maps.BATTERY_LOW);
               	Maps.batterywarning = true;
            }
 
		}
	};
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		isRunning = true;
	    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	    Log.d("WEYNAK.BATTERY_STAT", "battery service has been created");
	}

	@Override
	public void onDestroy() {
		this.unregisterReceiver(this.mBatInfoReceiver);
		isRunning = false;
		super.onDestroy();
		
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
		
}
